package com.online.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.music.common.Result;
import com.online.music.entity.po.*;
import com.online.music.utils.UserContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

import com.online.music.service.IUserFollowService;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-02-10
 * @Description: userFollow接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "userFollow接口")
@RestController
@RequestMapping("/user-follow")
public class UserFollowController {

    private final IUserFollowService userFollowService;

    @Operation(summary = "查询所有UserFollow")
    @GetMapping
    public Result<List<UserFollow>> findAll() {
        return Result.success(userFollowService.list());
    }

    @Operation(summary = "根据id查询UserFollow")
    @GetMapping("/{id}")
    public Result<UserFollow> findOne(@PathVariable Long id) {
        return Result.success(userFollowService.getById(id));
    }

    @Operation(summary = "根据分页查询UserFollow")
    @GetMapping("/page")
    public Result<Page<User>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                       UserFollow userFollow) {
        return userFollowService.findPage(pageNum, pageSize, userFollow);
    }

    @Operation(summary = "添加或修改UserFollow")
    @PostMapping
    public Result<Boolean> save(@RequestBody UserFollow userFollow) {
        return Result.success(userFollowService.saveOrUpdate(userFollow));
    }

    @Operation(summary = "根据id批量删除UserFollow")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return userFollowService.deleteBatch(ids);
    }

    // 查看有没有关注该用户
    @Operation(summary = "查看有没有关注该用户")
    @GetMapping("/is-follow/{followedUserId}")
    public Result<Boolean> isFollow(@PathVariable Long followedUserId) {
        UserFollow one = userFollowService.lambdaQuery()
                .eq(UserFollow::getUserId, UserContextUtil.getUserId())
                .eq(UserFollow::getFollowedUserId, followedUserId)
                .one();
        return Result.success(one != null);
    }

    // 关注
    @Operation(summary = "关注")
    @GetMapping("/follow/{followedUserId}")
    public Result<String> follow(@PathVariable Long followedUserId) {
        try {
            UserFollow userFollow = new UserFollow();
            userFollow.setUserId(UserContextUtil.getUserId());
            userFollow.setFollowedUserId(followedUserId);
            userFollowService.save(userFollow);
            return Result.successMsg("关注成功");
        } catch (Exception e) {
            return Result.error("关注失败,你已经关注过该用户");
        }
    }

    // 取消关注
    @Operation(summary = "取消关注")
    @DeleteMapping("/cancel/{followedUserId}")
    public Result<String> unfollow(@PathVariable Long followedUserId) {
        userFollowService.remove(
                new LambdaQueryWrapper<UserFollow>()
                        .eq(UserFollow::getUserId, UserContextUtil.getUserId())
                        .eq(UserFollow::getFollowedUserId, followedUserId)
        );
        return Result.successMsg("取消关成功");
    }

    // 关注的用户的歌单
    @Operation(summary = "关注用户的歌单")
    @GetMapping("/follow-playlist")
    public Result<Page<Playlist>> followPlaylist(UserFollow userFollow) {
        return userFollowService.followPlaylist(userFollow);
    }

    // 关注的用户的专辑
    @Operation(summary = "关注用户的专辑")
    @GetMapping("/follow-album")
    public Result<Page<Album>> followAlbum(UserFollow userFollow) {
        return userFollowService.followAlbum(userFollow);
    }

    // 关注的用户的歌曲
    @Operation(summary = "关注用户的歌曲")
    @GetMapping("/follow-song")
    public Result<Page<Song>> followSong(UserFollow userFollow) {
        return userFollowService.followSong(userFollow);
    }

}
